package com.boot.ys.mybatisplus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.ys.mybatisplus.entity.Abc;
import com.boot.ys.mybatisplus.entity.Dept;
import com.boot.ys.mybatisplus.entity.dto.DeptDto;
import com.boot.ys.mybatisplus.mapper.AbcMapper;
import com.boot.ys.mybatisplus.mapper.DeptMapper;
import com.boot.ys.mybatisplus.service.AbcService;
import com.boot.ys.mybatisplus.service.DeptService;
import com.boot.ys.mybatisplus.utils.TreeUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 杨升
 * @date: 2024-08-26 19:08
 * @Version 1.0
 */

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {


    public static void main(String[] args) {

        List<DeptDto> depts = new ArrayList<DeptDto>();

        DeptDto dept = new DeptDto();
        dept.setId(1);
        dept.setName("1");
        dept.setParentId(0);
        depts.add(dept);

        DeptDto dept1 = new DeptDto();
        dept1.setId(3);
        dept1.setName("3");
        dept1.setParentId(1);
        depts.add(dept1);


        DeptDto dept2 = new DeptDto();
        dept2.setId(2);
        dept2.setName("2");
        dept2.setParentId(0);
        depts.add(dept2);

        DeptDto dept3 = new DeptDto();
        dept3.setId(4);
        dept3.setName("4");
        dept3.setParentId(2);
        depts.add(dept3);

        DeptDto dept4 = new DeptDto();
        dept4.setId(5);
        dept4.setName("5");
        dept4.setParentId(4);
        depts.add(dept4);

        List<DeptDto> deptDtos = futureTaskTest(depts);

        System.out.println(deptDtos);


    }

    public static List<DeptDto> futureTaskTest(List<DeptDto> depts) {
        // List<DeptDto> depts = baseMapper.futureTask();


        List<DeptDto> treeList = depts.stream()
                .filter(dept -> (!Integer.valueOf(dept.getId()).equals(dept.getParentId())))
                .map(dept -> {
                    DeptDto node = new DeptDto();
                    node.setId(dept.getId());
                    node.setParentId(dept.getParentId());
                    node.setName(dept.getName());
                    return node;
                })
                .collect(Collectors.toList());

        return TreeUtil.build(treeList, 0);

    }


    @Override
    public List<DeptDto> futureTask() {
        List<DeptDto> depts = baseMapper.futureTask();

        List<DeptDto> treeList = depts.stream()
                .filter(dept -> (!Integer.valueOf(dept.getId()).equals(dept.getParentId())))
                .map(dept -> {
                    DeptDto node = new DeptDto();
                    node.setId(dept.getId());
                    node.setParentId(dept.getParentId());
                    node.setName(dept.getName());
                    return node;
                })
                .collect(Collectors.toList());

        TreeUtil.build(treeList, 0);


        return treeList;
    }

}
